package cn.jiyun.service.impl;

import cn.jiyun.entity.ChatMessage;
import cn.jiyun.service.PushService;
import cn.jiyun.util.ClientCacheUtil;
import com.corundumstudio.socketio.AckCallback;
import com.corundumstudio.socketio.SocketIOClient;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import static cn.jiyun.constant.Constants.CHAT_EVENT;
import static cn.jiyun.constant.Constants.PUSH_EVENT;
/**
 * @author admin
 */
@Service
public class PushServiceImpl implements PushService {
    /**
     * 推送给客户
     */
    @Override
    public void pushUser(String message, String userId) {
        HashMap<UUID, SocketIOClient> userClient = ClientCacheUtil.getUserClient(userId);
        System.out.println("111111"+userClient);
        if (userClient != null) {
            System.out.println("2222");
            userClient.forEach(((uuid, socketIOClient) -> {
                // 通过sendEvent给前端发送事件，并传递参数
                socketIOClient.sendEvent(PUSH_EVENT, new AckCallback<String>(String.class) {
                    @Override
                    public void onSuccess(String result) {
                        // 在这里处理客户端发送的确认消息
                        System.out.println("Received ack data from client: " + result);
                    }
                    @Override
                    public int getTimeout() {
                        return super.getTimeout();
                    }
                    @Override
                    public void onTimeout() {
                        super.onTimeout();
                    }
                }, message);
            }));
        }
        System.out.println("33333");
    }
    /**
     * 推送给多个客户
     */
    @Override
    public void pushUser(String message, List<String> userIdList) {

        HashMap<UUID, SocketIOClient> userClient = ClientCacheUtil.getUserClient(userIdList);

        userClient.forEach(((uuid, socketIOClient) -> {
            // 通过sendEvent给前端发送事件，并传递参数
            socketIOClient.sendEvent(PUSH_EVENT, message);
        }));


    }

    /**
     * 推送给客户 参数是pushMessage
     */
    @Override
    public void pushChatMessage(ChatMessage chatMessage) {
        String userId = chatMessage.getUserId();
        HashMap<UUID, SocketIOClient> userClient = ClientCacheUtil.getUserClient(userId);
        if (userClient != null) {
            userClient.forEach(((uuid, socketIOClient) -> {
                // 通过sendEvent给前端发送事件，并传递参数
//
                socketIOClient.sendEvent(CHAT_EVENT, new AckCallback<String>(String.class) {

                    @Override
                    public void onSuccess(String result) {
                        // 在这里处理客户端发送的确认消息
                        System.out.println("Received ack data from client: " + result);
                    }

                    @Override
                    public int getTimeout() {
                        return super.getTimeout();
                    }

                    @Override
                    public void onTimeout() {
                        super.onTimeout();
                    }
                }, chatMessage);


            }));


        }
    }
}

